package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

////• 定义 handle 处理函数
func healthz(w http.ResponseWriter, r*http.Request) {
	//for k, v := range r.URL.Query() {
	//	fmt.Fprintf(w, "ParamName %q, Value %q\n", k, v)
	//	fmt.Fprintf(w, "ParamName %q, Get Value %q\n", k, r.URL.Query().Get(k))
	//}
	for name, value := range r.Header {
		fmt.Printf("%v: %v\n", name, value)
	}

	// 遍历所有请求头
	for k, v := range r.Header {
		w.Header().Set(k,v[0])
		//fmt.Fprintf(w, "Header field %q, Value %q\n", k, v)
	}

	goPath := os.Getenv("GOPATH")
	//fmt.Printf("GOPATH is %s\n",goPath)
	w.Header().Set("GOPATH",goPath)

	fmt.Fprintf(w, "Method: %s URL: %s Protocol: %s \n", r.Method, r.URL, r.Proto)
	fmt.Fprintf(w, "Host = %q\n", r.Host)
	w.WriteHeader(200)
	fmt.Fprintf(w, "RemoteAddr= %q\n", r.RemoteAddr)
	// 通过 Key 获取指定请求头的值
	//fmt.Fprintf(w, "\n\nFinding value of \"Accept\" %q", r.Header["Accept"])

	// 返回 JSON 格式数据
	//greeting := Greeting{
	//	"欢迎访问学院君个人网站?",
	//}
	//message, _ := json.Marshal(greeting)
	//w.Write(message)
}

func main() {

	//environ := os.Environ()
	//for i := range environ {
	//	fmt.Println(environ[i])
	//}
	//fmt.Println("**************************")
	//goPath := os.Getenv("GOPATH")
	//fmt.Printf("GOPATH is %s\n",goPath)

	http.HandleFunc("/", handler)
	//注册 handle 处理函数
	http.HandleFunc("/healthz", healthz)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Fatal(err)
	}

	//log.Fatal(http.ListenAndServe(":8080", nil))
}